import bb.cascades 1.2
import bb.system 1.2
import bb.data 1.0

ListView {
    signal refreshTriggered()
    signal loadmoreTriggered()
    property bool loading: false
    property int dataTotal: 0

    id: refreshableList

    leadingVisualSnapThreshold: 2.0
    leadingVisual: RefreshHeader {
        id: refreshHandler
        onRefreshTriggered: {
            refreshableList.clear();
            refreshableList.refreshTriggered();
        }
    }

    function insertList(items) {
        theDataModel.insertList(items)
        dataTotal = theDataModel.size();
    }

    function insert(item) {
        theDataModel.insert(item)
        dataTotal = theDataModel.size();
    }

    function clear() {
        theDataModel.clear();
    }

    function startLoad() {
        loading = true;

        var loadmore = {
            "itemRoot": "zfooter",
            "itemType": "zfooter",
            "title": "正在加载...",
            "isloading": true,
            "isopt": false
        }

        if (theDataModel.size() == 0) {
            theDataModel.insert(loadmore)
            return;
        }

        var indexPath = theDataModel.last();
        theDataModel.updateItem(indexPath, loadmore)
    }

    function stopLoad() {
        loading = false;
        //        refreshableList.scrollToItem([ dataTotal - 2 ], ScrollAnimation.None);

        var loadmore = {
            "itemRoot": "zfooter",
            "itemType": "zfooter",
            "title": "点击加载更多",
            "isloading": false,
            "isopt": true
        }

        var indexPath = theDataModel.last()
        var last = theDataModel.data(indexPath)

        if (theDataModel.size() == 0 || last.itemType == "dataitem") {
            theDataModel.insert(loadmore)
        } else {
            theDataModel.updateItem(indexPath, loadmore)
        }
    }

    function loadFinished() {
        var indexPath = theDataModel.last()
        var last = theDataModel.data(indexPath)

        if (last.itemType == "zfooter") {
            var loadok = {
                "itemRoot": "zfooter",
                "itemType": "zfooter",
                "title": "所有数据已加载完毕",
                "isloading": false,
                "isopt": false
            }

            theDataModel.updateItem(indexPath, loadok)
        }
    }
    
    function scrollBegin(){
        scrollToPosition(ScrollPosition.Beginning, ScrollAnimation.None);
    }

    onTriggered: {
        var currentItem = theDataModel.data(indexPath);
        if (currentItem.itemType && currentItem.itemType == "zfooter") {
            //脚部
            if (currentItem.isopt) {
                refreshableList.loadmoreTriggered();
            }
        }
    }

    dataModel: GroupDataModel {
        id: theDataModel
        grouping: ItemGrouping.None
        sortedAscending: true
        sortingKeys: [ "itemRoot", "index" ]
    }

    attachedObjects: [
        ListScrollStateHandler {
            onAtEndChanged: {
                if (! loading && atEnd) {
                    refreshableList.loadmoreTriggered();
                }
            }
        }
    ]

    onTouch: {
        refreshHandler.onListViewTouch(event);
    }

    function itemType(data, indexPath) {
        return data.itemType;
    }

    onLoadingChanged: {
        refreshHandler.refreshing = refreshableList.loading;
    }

    listItemComponents: [
        ListItemComponent {
            type: "zfooter"
            CustomListItem {
                dividerVisible: false
                LoadMoreItem {
                    title: ListItemData.title
                    isloading: ListItemData.isloading
                }
            }
        }
    ]
}
